package com.elitel.frame.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.elitel.common.core.domain.JsonResult;
import com.elitel.common.utils.RedisUtils;
import com.elitel.frame.business.annotation.DataCache;
import com.elitel.frame.business.service.common.impl.CommonServerExecutor;
import com.elitel.frame.main.entity.SysPrjservice;
import com.elitel.frame.main.entity.vo.JsonDataResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

import static com.elitel.common.constant.RedisKeyConstant.SERVER_INFO_KEY;
import static com.elitel.common.core.constant.RequestConstant.GUID;
import static com.elitel.common.core.constant.RequestConstant.SERVICE_ID;
import static com.elitel.frame.main.entity.constant.CommonApiUrl.*;

/**
 * 通用接口入口 禁止在此类中添加业务代码
 *
 * @author mulis
 */
@RestController
@RequestMapping(COMMON)
public class CommonController {
    private static final Logger logger = LoggerFactory.getLogger(CommonController.class);

    @Resource(name = "commonServerExecutor")
    private CommonServerExecutor commonServerExecutor;

    @PostMapping(API)
    public JsonDataResult api(@RequestBody(required = false) Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {

        try {
            if (params == null) {
                params = new HashMap<>();
            }
            Object res = commonServerExecutor.request(params, request, response);
            // 防止在service就封装返回值的情况
            if (res instanceof JsonDataResult) {
                return (JsonDataResult) res;
            } else {
                JsonDataResult jsonDataResult = new JsonDataResult();
                jsonDataResult.setResult(0);
                jsonDataResult.setData(res);
                return jsonDataResult;
            }
        } catch (Exception e) {
            logger.error("请求异常", e);
            JsonDataResult jsonDataResult = new JsonDataResult();
            jsonDataResult.setResult(500);
            jsonDataResult.setMessage(e.getMessage());
            return jsonDataResult;
        }
    }

    @PostMapping(API + "/{serviceGuid}")
    @DataCache(serviceGuidField = "serviceGuid")
    public JsonDataResult apiPath(@PathVariable("serviceGuid") String serviceGuid, @RequestBody(required = false) Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {

        if (params == null) {
            params = new HashMap<>();
        }
        try {
            Object o = RedisUtils.hGet(SERVER_INFO_KEY, serviceGuid);
            if (o == null) {
                return JsonDataResult.responseError("服务不存在");
            } else {
                SysPrjservice sysPrjservice = JSONObject.parseObject(o + "", SysPrjservice.class);
                params.put(SERVICE_ID, sysPrjservice.getServicecode());
            }
            Object res = commonServerExecutor.request(params, request, response);
            // 防止在service就封装返回值的情况
            if (res instanceof JsonDataResult) {
                return (JsonDataResult) res;
            } else {
                JsonDataResult jsonDataResult = new JsonDataResult();
                jsonDataResult.setResult(0);
                jsonDataResult.setData(res);
                return jsonDataResult;
            }
        } catch (Exception e) {
            logger.error("请求异常", e);
            JsonDataResult jsonDataResult = new JsonDataResult();
            jsonDataResult.setResult(500);
            jsonDataResult.setMessage(e.getMessage());
            return jsonDataResult;

        }
    }

    @PostMapping(JSON_RESULT_API + "/{serviceGuid}")
    @DataCache(serviceGuidField = "serviceGuid")
    public Object jsonResultApi(@PathVariable("serviceGuid") String serviceGuid, @RequestBody(required = false) Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {

        if (params == null) {
            params = new HashMap<>();
        }
        try {
            Object o = RedisUtils.hGet(SERVER_INFO_KEY, serviceGuid);
            if (o == null) {
                return JsonResult.error("服务不存在");
            } else {
                SysPrjservice sysPrjservice = JSONObject.parseObject(o + "", SysPrjservice.class);
                params.put(SERVICE_ID, sysPrjservice.getServicecode());
                params.put(GUID, serviceGuid);
            }
            // 防止在service就封装返回值的情况
            return commonServerExecutor.request(params, request, response);
        } catch (Exception e) {
            logger.error("请求异常", e);
            return JsonResult.error(e.getMessage());
        }
    }


}
